//
//  SEGActivitySignBubbleButton.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/8/25.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGActivitySignBubbleButton.h"
#import "SEGActivityManager.h"

#define kActivityTaskBubbleCloseDate @"kActivityTaskBubbleCloseDate"

@interface SEGActivitySignBubbleButton()

/// 气泡按钮
@property (nonatomic, strong) UIButton *bubbleButton;
/// 关闭按钮
@property (nonatomic, strong) UIButton *closeButton;

@end

@implementation SEGActivitySignBubbleButton

+ (SEGActivitySignBubbleButton *)sharedView
{
    static SEGActivitySignBubbleButton *_instance;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        _instance = [[self alloc] initWithFrame:CGRectZero];
    });
    return _instance;
}


//+ (void)showSignBubbleWithBlock:(ActicitySignBubbleButtonBlock)block
+ (void)showSignBubbleInView:(UIView *)view block:(ActicitySignBubbleButtonBlock)block
{
    [[SEGActivitySignBubbleButton sharedView] showWithBlock:block view:view];
}

+ (void)hiddenSignBubble
{
    [[SEGActivitySignBubbleButton sharedView] hiddenBubble];
}

#pragma mark -初始化
-(instancetype)init{
    if (self == [super init]) {
        [self seg_setupUI];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        [self seg_setupUI];
    }
    return self;
}

-(void)seg_setupUI{
    CGFloat closeButtonWidth = 30;
    UIImage* signImage = [UIImage imageWithContentsOfFile:SEGPicName(@"icon_checkin")];
    self.bubbleButton = ({
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setBackgroundImage:signImage forState:UIControlStateNormal];
        [button addTarget:self action:@selector(seg_pressed) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        button.titleLabel.font = kFont_10;
        button.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 3, 0);
        button.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
        [self addSubview:button];
        button;
    });
    [self.bubbleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.mas_equalTo(0);
        make.width.mas_equalTo(signImage.size.width);
        make.height.mas_equalTo(signImage.size.height);
    }];
    
    self.closeButton = ({
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:SEGAutoPicName(@"icon_home_delete") forState:UIControlStateNormal];
        button.hidden = YES;
        [self addSubview:button];
        button;
    });
    [self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.mas_equalTo(0);
        make.width.height.mas_equalTo(closeButtonWidth);
    }];
    [[self.closeButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        self.hidden = YES;
        [self hiddenTaskBubble];
    }];
}

- (void)seg_pressed
{
    if (self.pressblock) {
        self.pressblock();
    }
}

- (void)showWithBlock:(ActicitySignBubbleButtonBlock)block view:(UIView *)view
{
    self.pressblock = block;
    [view addSubview:self];
    
    //    [[[UIApplication sharedApplication] keyWindow] addSubview:self];
    //
    //    //防止签到按钮显示在升级弹窗之上
    //    [[[UIApplication sharedApplication] keyWindow].subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    //        if ([obj isKindOfClass:NSClassFromString(@"SEGUpdateAppView")]) {
    //           [[[UIApplication sharedApplication] keyWindow] bringSubviewToFront:obj];
    //            *stop = YES;
    //        }
    //    }];
}

- (void)displaySignBubble {
    [self.bubbleButton setBackgroundImage:[UIImage imageWithContentsOfFile:SEGPicName(@"icon_checkin")] forState:UIControlStateNormal];
    [self.bubbleButton setTitle:@"" forState:UIControlStateNormal];
    self.closeButton.hidden = YES;
}

- (void)displayTaskBubbleWithCountString:(NSString *)countString {
    [self.bubbleButton setBackgroundImage:[UIImage imageWithContentsOfFile:SEGPicName(@"icon_task")] forState:UIControlStateNormal];
    [self.bubbleButton setTitle:countString forState:UIControlStateNormal];
    self.closeButton.hidden = NO;
}

- (void)hiddenBubble
{
    [self removeFromSuperview];
}

/// 本地存任务气泡关闭状态
- (void)hiddenTaskBubble {
    NSDateFormatter *formatter = [NSDateFormatter new];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSString *currentDateString = [formatter stringFromDate:[NSDate date]];
    [[NSUserDefaults standardUserDefaults] setObject:currentDateString forKey:kActivityTaskBubbleCloseDate];
}

/// 检查是否已经关闭过任务气泡
+ (BOOL)weatherAlreayCloseTaskBubble {
    NSString *lastCloseDate = [[NSUserDefaults standardUserDefaults] objectForKey:kActivityTaskBubbleCloseDate];
    if (lastCloseDate.length > 0) {
        NSDateFormatter *formatter = [NSDateFormatter new];
        formatter.dateFormat = @"yyyy-MM-dd";
        NSString *currentDateString = [formatter stringFromDate:[NSDate date]];
        if ([lastCloseDate isEqualToString:currentDateString]) {
            return YES;
        }
        // 未关闭或到第二日则状态置空
        [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kActivityTaskBubbleCloseDate];
        return NO;
    }
    return NO;
}

@end
